package com.itcast.n6_方法区;

import net.bytebuddy.jar.asm.ClassWriter;
import net.bytebuddy.jar.asm.Opcodes;

import java.io.IOException;

/**
 * @program: jvm
 * @description: -c
 * @author: flyduck
 * @create: 2024-09-23 11:06
 **/
//java.lang.OutOfMemoryError: Metaspace
public class Test02_使用MaxMetaspaceSize控制方法区最大内存 extends ClassLoader{
    public static void main(String[] args) throws IOException {
        System.in.read();
        Test02_使用MaxMetaspaceSize控制方法区最大内存 test01_模拟方法区的溢出 = new Test02_使用MaxMetaspaceSize控制方法区最大内存();
        int count = 0;
        while (true) {
            String name = "Class" + count;
            ClassWriter classWriter = new ClassWriter(0);
            classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, name, null
                    , "java/lang/Object", null);
            byte[] bytes = classWriter.toByteArray();
            test01_模拟方法区的溢出.defineClass(name, bytes, 0, bytes.length);
            System.out.println(++count);
        }
    }
}
